import React, {PureComponent} from "react";
import FormEditor from '../../components/FormEditor';
import {updateOrderModel} from "./actions";
import {connect} from "react-redux";
import {getPrincipal} from "../../lib/identity";

class OrderInfoModal extends PureComponent {

    handleUpdateCarrierModel = order => {
        const {dispatch, model} = this.props;
        dispatch(updateOrderModel({...model, ...order, "id": model.id}));
    };

    render() {
        const {model, clientList, calculateType, businessCodeList, lines, vehicles, organizations} = this.props;
        const originDestPlaceList = [];
        model.originDestPlace = model.originPlace + "-" + model.destPlace;
        lines.forEach(item => {
            const originDestPlace = item.originPlace + "-" + item.destPlace;
            const found = originDestPlaceList.find(item => item.value === originDestPlace);
            if (!found) {
                originDestPlaceList.push({label: originDestPlace, value: originDestPlace});
            }
        });
        const schema = [
            {
                title: '基础信息',
                fields: [
                    {
                        field: 'clientId',
                        title: '客户',
                        type: 'listSelector',
                        readonly: model.status === "CONFIRMED",
                        controlProps: {
                            dataSource: clientList,
                            labelField: "name",
                            valueField: "id",
                            onChange: this.changeClient
                        },
                        fieldOptions: {
                            rules: [{required: true, message: '请选择客户'}]
                        }
                    }, {
                        field: 'businessCode',
                        title: '业务类型',
                        type: 'listSelector',
                        readonly: model.status === "CONFIRMED",
                        controlProps: {
                            dataSource: businessCodeList,
                            labelField: "name",
                            valueField: "name"
                        },
                        fieldOptions: {
                            rules: [{required: true, message: '请选择业务类型'}]
                        }
                    }, {
                        field: 'calculateType',
                        title: '计费方式',
                        type: 'listSelector',
                        readonly: model.status === "CONFIRMED",
                        controlProps: {
                            dataSource: calculateType
                        },
                        fieldOptions: {
                            rules: [{required: true, message: '请选择计费方式'}]
                        }
                    }, {
                        field: 'bookNumber',
                        title: '订舱号',
                        readonly: model.status === "CONFIRMED",
                        type: 'text',
                        fieldOptions: {
                            rules: [{required: true, message: '请选择订舱号'}]
                        }
                    }, {
                        field: 'pickDate',
                        title: '提货日期',
                        readonly: model.status === "CONFIRMED",
                        type: 'date'
                    }, {
                        field: 'actDepartureTime',
                        title: '发车日期',
                        readonly: model.status === "CONFIRMED",
                        type: 'date'
                    }, {
                        field: 'quantity',
                        title: '数量',
                        readonly: model.status === "CONFIRMED",
                        type: 'number'
                    }, {
                        field: 'squareNumber',
                        title: '方数(m³)',
                        readonly: model.status === "CONFIRMED",
                        type: 'number'
                    }, {
                        field: 'grossWeight',
                        title: '毛重(KG)',
                        readonly: model.status === "CONFIRMED",
                        type: 'number'
                    }, {
                        field: 'pbText',
                        title: '重泡比',
                        readonly: true
                    }, {
                        field: 'volumeWeight',
                        title: '体积重(KG)',
                        readonly: true
                    }, {
                        field: 'originDestPlace',
                        title: '行邮线路',
                        readonly: model.status === "CONFIRMED",
                        type: 'listSelector',
                        controlProps: {
                            dataSource: originDestPlaceList
                        },
                        fieldOptions: {
                            rules: [{required: true, message: '请选择行邮线路'}]
                        }
                    }, {
                        field: 'receiveDate',
                        title: '派送日期',
                        readonly: model.status === "CONFIRMED",
                        type: 'date'
                    }
                ]
            }, {
                title: '提货信息',
                fields: [
                    {
                        field: 'vehicleType',
                        title: '提货车型',
                        readonly: model.status === "CONFIRMED",
                        type: 'listSelector',
                        controlProps: {
                            dataSource: vehicles,
                            labelField: "name",
                            valueField: "name",
                            onChange: this.changeVehicleType
                        }
                    }, {
                        field: 'vehicleLine',
                        title: '提货线路',
                        readonly: true
                    }
                ]
            }, {
                title: '派送信息',
                fields: [
                    {
                        field: 'deliveryModel',
                        title: '派送车型',
                        readonly: model.status === "CONFIRMED",
                        type: 'listSelector',
                        controlProps: {
                            dataSource: vehicles,
                            labelField: "name",
                            valueField: "name",
                            onChange: this.changeDeliveryModel
                        }
                    }, {
                        field: 'deliveryLine',
                        title: '派送线路',
                        readonly: true
                    }, {
                        field: 'deliveryModelRemark',
                        title: '派送备注',
                        readonly: model.status === "CONFIRMED",

                    }
                ]
            }, {
                title: '备注信息',
                fields: [
                    {
                        field: 'remark',
                        title: '备注',
                        type: 'textArea',
                        readonly: model.status === "CONFIRMED",

                    },
                    {
                        field: 'billRemark',
                        title: '账单备注',
                        type: 'textArea',
                        readonly: model.status === "CONFIRMED",

                    }
                ]
            }
        ];

        if (getPrincipal().admin) {
            schema[0].fields.splice(0, 0, {
                field: 'organizationId',
                title: '所属机构',
                type: 'treeSelector',
                readonly: model.status === "CONFIRMED",
                controlProps: {
                    treeData: organizations,
                    labelField: "name",
                    valueField: "id",
                    treeCheckable: false
                },
                fieldOptions: {
                    rules: [{required: true, message: '请选择所属机构'}]
                }
            });
        }

        return (

            <FormEditor
                schema={schema}
                column={3}
                defaultReadonly={true}
                showActionBar={false}
                defaultValues={model}
                onChange={this.handleUpdateCarrierModel}
            />
        );
    }
}

const mapStateToProps = state => {
    return {
        clientList: state.home.clients,
        carrierList: state.home.carriers,
        currencyList: state.ghOrderPayable.list.currencyList,
        calculateType: state.common.calculateType,
        vehicles: state.vehicle.list.dataSource,
        organizations: state.home.organizations,
        businessCodeList: state.ghOrderPayable.list.businessCodeList,
        lines: state.ghOrder.lines.lines
    };
};

export default connect(mapStateToProps)(OrderInfoModal);
